package com.nbstar.film.spider.spider;

import com.nbstar.film.spider.http.BaseHttpRequest;
import com.nbstar.film.spider.http.FirefoxDriverRequest;
import com.nbstar.film.spider.http.SipderStringRequest;
import com.nbstar.film.spider.pojo.FilmInfo;
import com.nbstar.film.spider.filter.IMediaNameFilter;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

@Slf4j
@Data
public abstract class BaseSpider implements FilmSpider {
    //private static Logger logger = LoggerFactory.getLogger(BaseSpider.class);
    //protected List<ProxyInfo> proxyInfoList = new ArrayList<>();

    private BaseHttpRequest request = new SipderStringRequest();

    protected FirefoxDriverRequest firefoxDriverRequest = null;

    protected String spiderName = "";

    protected List<IMediaNameFilter> filters = new ArrayList<>();

    private  int retry = 0;

    public String getSpiderName() {
        return spiderName;
    }

    public BaseHttpRequest getRequest() {
        return request;
    }

    public BaseHttpRequest getProxyRequest() {
        request.getProxy();
        return request;
    }

    public void setRequest(BaseHttpRequest request) {
        this.request = request;
    }

    public void registFilter(IMediaNameFilter filter) {
        filters.add(filter);
    }

    public void setRetry(int r) {
        this.retry = r;
    }

    protected String  parseName(String filmName) {
        String name = filmName;

        for(IMediaNameFilter filter : filters) {
            name = filter.parseName(name);
        }

        return name;
    }

    public FilmInfo run(String filmName) {
        FilmInfo filmInfo = new FilmInfo();

        try {

            if (null == filmName) {
                return filmInfo;
            }

            String name = parseName(filmName);

            String contentId = getContentId(name);
            int count = retry;

            while (StringUtils.isEmpty(contentId)  && count > 0) {
                System.out.println("[" + filmName + "]" + " found null contentId, sleep to try again!");
                Thread.sleep(10000);
                count--;
                contentId = getContentId(filmName);

            }

            filmInfo = getFilmInfo(contentId);
            filmInfo.setCname(filmName);
            if (firefoxDriverRequest != null) {
               // FirefoxDriver fir  = (FirefoxDriver) firefoxDriverRequest.getDriver();
                //fir.resetInputState();
            }
            return filmInfo;
        } catch (Exception e) {
            log.error("Exception at file:{}, spider:{}", filmName, getSpiderName() );
            e.printStackTrace();
        }

        return filmInfo;
    }

    public FilmInfo runProxy(String filmName) {
        request.setSpiderName(this.spiderName);
        request.getProxy();
        String contentId = null;
        FilmInfo filmInfo = new FilmInfo();
        String name = parseName(filmName);

        int count = retry;

        do {
            try {

                contentId = getContentId(name);

                if (StringUtils.isEmpty(contentId)) {
                    log.info(getRequest().getProxyInfo().getProxy() + " is unable!");
                    request.deleteProxy();
                    request.getProxy();
                    count--;
                    continue;
                }
            } catch (Exception e) {
                log.info(getRequest().getProxyInfo().getProxy() + " is unable!" + e.getMessage());
                request.deleteProxy();
                request.getProxy();
                count--;
                continue;
            }

            count--;
        } while (StringUtils.isEmpty(contentId) &&  count>0);

        try {
            filmInfo = getFilmInfo(contentId);
        } catch (Exception e) {
            log.info(getRequest().getProxyInfo().getProxy() + " is unable!" + e.getMessage());
            request.deleteProxy();
            request.getProxy();
        }

        return filmInfo;
    }

}
